<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %>
<div class="page_right equipmentpage_right">
    <div class="equipment_rightp">
        <span class="equipment_rightp_text1">${deviceTypeName}</span>
        <span class="equipment_rightp_text2" name="0">${groupName}</span>
        <a class="equipment_parameter_addbtn1" href="javascript:;">增加参数</a>
        <a class="equipment_parameter_addbtn2" href="javascript:;">增加参数组</a>
        <a class="equipment_parameter_addbtn3" href="javascript:;">修改参数组名称</a>
        <div class="equipement_factory_details_editdv" name="0">
            <input type="text" class="equipement_factory_details_editval" placeholder="请输入参数组名称">
            <a href="javascript:;" class="equipement_factory_details_editbtn1">确认</a>
            <a href="javascript:;" class="equipement_factory_details_editbtn2">取消</a>
        </div>
    </div>
    <ul class="equipment_parameter_rightTitle">
        <li>序号</li>
        <li>参数名称</li>
        <li>图片</li>
        <li>命令编号</li>
        <li>参数单位</li>
        <li>参数范围</li>
        <li>参数值</li>
        <li>读写权限</li>
        <li>参数说明</li>
        <li>参数视频链接</li>
        <li>操作</li>
    </ul>

    <div class="equipment_parameter_rightContentdv">
        <c:forEach items="${configList}" var="data" varStatus="status">
            <ul class='equipment_parameter_rightContent'>
                <li>${status.count}</li>
                <li>${data.devparamname}</li>
                <li><img class="equipment_parameter_img" src="${data.paramimageurl}" alt="图片"></li>
                <li>${data.commid}</li>
                <li>${data.devparamunit}</li>
                <li><span>${data.indevparamval}</span>~<span>${data.outdevparamval}</span></li>
                <li>${data.devparamdefval}</li>
                <li name="${data.sdata2}">${data.sdata2==0?"只读":"可读写"}</li>
                <li>${data.remark}</li>
                <li>${data.paramvidiourl}</li>
                <li>
                    <a class="equipment_parameter_edit" href="javascript:;" name="${data.confparamid}">修改参数</a>
                </li>
            </ul>
        </c:forEach>
    </div>
    <div class="page_line"></div>
</div>

<!-- 新增参数组 提示弹窗 -->
<div class="pointpage_dv2" style="display: none;">
    <div class="pointpage_psd">
        <p class="pointpage_psdtitle">
            <strong class="pointpage_psdtitleName">增加参数组</strong>
            <a href="javascript:;" class="pointpage_psdclose"></a>
        </p>
        <p class="pointpage_psdval">
            <span>类型参数组：</span>
            <input class="pointpage_value" type="text" placeholder="请输入类型参数组名称">
            <a class="equipment_parameter_cfm" href="javascript:;">确认</a>
        </p>
        <p class="pointpage_point">请输入1-6位字符</p>

    </div>
</div>
<!-- 新增参数组 提示弹窗 end -->

<!-- 新增参数项   修改参数   提示弹窗 -->
<div class="pointpage_dv3" style="display: none;">
    <div class="equipment_parameter_pointpage">
        <p class="pointpage_psdtitle">
            <strong class="pointpage_psdtitleName">增加参数</strong>
            <a href="javascript:;" class="pointpage_psdclose"></a>
        </p>
        <div class="equipment_parameter_newdv">
            <dl class="equipment_parameter_newdl1">
                <dt>参数名称：</dt>
                <dd><input class="equipment_parameter_newdd1" type="text" placeholder="请输入参数名称" value=""></dd>
                <dt>参数范围（0~65535）：</dt>
                <dd><input type="text" value="0" class="equipment_parameter_newdd2">&nbsp;~&nbsp;<input type="text" class="equipment_parameter_newdd3" value=""></dd>
            </dl>

            <dl class="equipment_parameter_newdl2">
                <dt>参数单位：</dt>
                <dd><input type="text" class="equipment_parameter_newdd4" placeholder="请输入参数名称" value=""></dd>
                <dt>参数值：</dt>
                <dd><input type="text" class="equipment_parameter_newdd5" placeholder="请输入参数值" value=""></dd>
            </dl>

            <dl class="equipment_parameter_newImg">
                <dt>参数图片：</dt>
                <dd id="GC_show_file_dd">
                    <img id="GC_show_file" src="${basePath}resources/images/web/v2/index/addImg.png" alt="">
                    <input id="GC_logo_file" type="file" class="equipment_parameter_newImgFile">
                </dd>
            </dl>
            <dl class="equipment_parameter_newdl2" style="height:110px;">
                <dt>命令编号${isOperator==0?"(1500~2000)":"(500~800)"}：</dt>
                <dd><input type="text" class="equipment_parameter_newdd8" name="${isOperator}" placeholder="请输入参数名称" value=""></dd>
            </dl>
            <dl class="equipment_parameter_newPower">
                <dt>权限选择：</dt>
                <dd>
                    <input type="radio" checked="checked" name="read" id="read1" value="0">
                    <label for="read1">只读</label>
                    <span>（设备只会上传参数到服务器，而服务器不能下发修改该参数）</span>
                </dd>
                <dd>
                    <input type="radio" name="read" id="read2" value="1">
                    <label for="read2">可读写</label>
                    <span>（设备可上传参数到服务器，而服务器也可下发修改该参数）</span>
                </dd>
            </dl>

            <dl class="equipment_parameter_newdl3">
                <dt>参数说明：</dt>
                <dd><textarea class="equipment_parameter_newdd6" placeholder="请输入参数说明" value=""></textarea></dd>
                <dt>参数视频链接：</dt>
                <dd><textarea class="equipment_parameter_newdd7" placeholder="请输入参数视频链接网址" value=""></textarea></dd>
            </dl>

            <p class="equipment_parameter_newp">
                <input type="button" disabled="disabled" class="equipment_parameter_newbtn1" value="确认">
                <input type="button" class="equipment_parameter_newbtn2" value="取消">
            </p>
        </div>

    </div>
</div>
<!-- 新增参数项  修改参数  提示弹窗 end -->

<script type="text/javascript">
    //图片上传
    var defaultlogo = "${basePath}resources/images/web/v2/index/addImg.png";
    $(".equipment_parameter_newImgFile").change(function () {
        var files = this.files,img = new Image();
        if(window.URL) {
            var file = files[0];
            img.src = window.URL.createObjectURL(file);
            img.onload = function(e) {
                window.URL.revokeObjectURL(this.src); //图片加载后，释放object URL
            }
            $("#GC_show_file").attr("src", img.src);
        }else if(window.FileReader){
            var file = files[0];
            var reader = new FileReader();
            reader.readAsDataURL(file);
            reader.onload = function(e){
                img.src = this.result;
            }
            $("#GC_show_file").attr("src", img.src);
        }else{
            $("#GC_show_file").hide();
            $("#GC_logo_file").select();
            $("#GC_logo_file").blur();
            var newPreview = document.getElementById("GC_new_show_file");
            if (newPreview == null) {
                newPreview = document.createElement("div");
                newPreview.setAttribute("id", "GC_new_show_file");
            }
            newPreview.style.width = $("#GC_show_file").width()+"px";
            newPreview.style.height =  $("#GC_show_file").height()+"px";
            newPreview.style.filter ="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale',src='" + document.selection.createRange().text + "')";
            newPreview.style.display = "block";
            document.getElementById("GC_show_file").style.display = "none";
            document.getElementById("GC_showlogo_span").appendChild(newPreview);
        }

    });


    //参数范围、参数值输入限定
    $(".equipment_parameter_newdd2, .equipment_parameter_newdd3, .equipment_parameter_newdd5").on("keyup keydown", function(){
        var equipment_parameter_newValue=parseInt($(this).val());
        if(equipment_parameter_newValue>=0){
            $(this).val(equipment_parameter_newValue);
        }else{
            $(this).val("");
        }
    });


    //增加参数  显示
    $(".equipment_parameter_addbtn1").on("click", function(){
        $("#GC_show_file").attr("src", defaultlogo);
        $(".equipment_parameter_newImgFile").val("");
        $(".equipment_parameter_newdd1, .equipment_parameter_newdd2, .equipment_parameter_newdd3, .equipment_parameter_newdd4, .equipment_parameter_newdd5, .equipment_parameter_newdd6, .equipment_parameter_newdd7, .equipment_parameter_newdd8").val("");
        $(".equipment_parameter_newPower input:eq(0)")[0].checked=true;
        $(".pointpage_dv3").stop().show().find(".pointpage_psdtitleName").text("${deviceTypeName}> ${groupName}> 增加参数");
        //增加参数组  确认按钮解禁
        $(".equipment_parameter_newdd1").on("keyup keydown", function(){
            if(regName.test($(this).val())){
                $(".equipment_parameter_newbtn1").removeAttr("disabled", "disabled");
            }else{
                $(".equipment_parameter_newbtn1").attr("disabled", "disabled");
            }
        });
        //增加参数  确认
        var reg_empty=/^\s*$/;
        $(".equipment_parameter_newbtn1").unbind("click");
        $(".equipment_parameter_newbtn1").on("click", function(){
            var typeName = "${deviceParamType}";
            var groupName = "${groupId}";
            var name = $(".equipment_parameter_newdd1").val();
            var img="图片";
            var commId=$(".equipment_parameter_newdd8").val();
            var inVal = $(".equipment_parameter_newdd2").val();
            var outVal = $(".equipment_parameter_newdd3").val();
            var unit = $(".equipment_parameter_newdd4").val();
            var defVal = $(".equipment_parameter_newdd5").val();
            var remark = $(".equipment_parameter_newdd6").val();
            var urlVal = $(".equipment_parameter_newdd7").val();
            var permissions = $(".equipment_parameter_newPower input:checked").val();
            var permissionsText = $(".equipment_parameter_newPower input:checked").next("label").text();
            var isOperator = $(".equipment_parameter_newdd8").attr("name");
            var equipment_parameter_newArr=[
                $(".equipment_parameter_rightContentdv>ul").length+1,
                name,img,commId,unit,outVal,defVal,permissions,remark,urlVal,
                "修改参数"
            ];

            if(reg_empty.test(unit)){
                alert("参数单位不能为空");
                return;
            }else if(reg_empty.test(inVal)){
                alert("参数范围不能为空");
                return;
            }else if(reg_empty.test(outVal)){
                alert("参数范围不能为空");
                return;
            }else if(reg_empty.test(defVal)){
                alert("参数值不能为空");
                return;
            } else if(reg_empty.test(commId)){
                alert("命令编号不能为空");
                return;
            }

            var forData = new FormData();
            forData.append("typeName",typeName);
            forData.append("groupName",groupName);
            forData.append("name",name);
            forData.append("inVal",inVal);
            forData.append("outVal",outVal);
            forData.append("unit",unit);
            forData.append("defVal",defVal);
            forData.append("commId",commId);
            forData.append("remark",remark);
            forData.append("urlVal",urlVal);
            forData.append("permissions",permissions);
            forData.append("isOperator",isOperator);
            forData.append('logo', $('#GC_logo_file')[0].files[0]);

            $.ajax({
                url: "${basePath}v2/factory/param/addParam",
                type: 'POST',
                data: forData,
                cache: false,
                contentType: false,
                processData: false,
                error: function (response) {
                    $errorMsg(response);
                },
                success: function (ret) {
                    if(ret.result==true){
                        var logo = ret.logo;
                        var command = ret.command;
                        var confParamId = ret.confParamId;
                        var equipment_parameter_allLi="";
                        for(var i=0; i<11; i++){
                            if(i==2){
                                equipment_parameter_allLi+="<li><img class='equipment_parameter_img' src='"+logo+"' alt='图片'></li>";
                            }else if(i==3){
                                equipment_parameter_allLi+="<li>"+command+"</li>";
                            }else if(i==5){
                                equipment_parameter_allLi+="<li><span>"+inVal+"</span>~<span>"+equipment_parameter_newArr[i]+"</span></li>";
                            }else if(i==7){
                                equipment_parameter_allLi+="<li name='"+equipment_parameter_newArr[i]+"'>"+permissionsText+"</li>";
                            }else if(i==10){
                                equipment_parameter_allLi+="<li><a class='equipment_parameter_edit' href='javascript:;' name='"+confParamId+"'>"+equipment_parameter_newArr[i]+"</a></li>";
                            }else{
                                equipment_parameter_allLi+="<li title='"+equipment_parameter_newArr[i]+"'>"+equipment_parameter_newArr[i]+"</li>";
                            }
                        }
                        $(".equipment_parameter_rightContentdv").append("<ul class='equipment_parameter_rightContent'>"+equipment_parameter_allLi+"</ul>");
                        $(".pointpage_dv3").stop().hide();
                        equipment_parameter_newArr=null;
                        equipment_parameterEdit();
                    }else{
                        alert(ret.info);
                    }
                }
            });
        });
    });

    //修改参数  显示
    var equipment_index;
    function equipment_parameterEdit(){
        $(".equipment_parameter_edit").on("click", function(){
            equipment_index=$(this).parent().parent().index();
            var configParam = $(this).attr("name");
            //取出该项的参数数据
            var equipment_parameter_newArr=[
                $(this).parents(".equipment_parameter_rightContent").children().eq(1).text(),
                $(this).parents(".equipment_parameter_rightContent").children().eq(4).text(),
                $(this).parents(".equipment_parameter_rightContent").children().eq(5).children("span").eq(0).text(),
                $(this).parents(".equipment_parameter_rightContent").children().eq(5).children("span").eq(1).text(),
                $(this).parents(".equipment_parameter_rightContent").children().eq(6).text(),
                $(this).parents(".equipment_parameter_rightContent").children().eq(8).text(),
                $(this).parents(".equipment_parameter_rightContent").children().eq(9).text(),
                $(this).parents(".equipment_parameter_rightContent").children().eq(3).text(),
                $(this).parents(".equipment_parameter_rightContent").children().eq(7).attr("name")
            ];
            //弹窗赋值 修改项的参数数据
            $(".equipment_parameter_newdd1").val(equipment_parameter_newArr[0]);
            $(".equipment_parameter_newdd2").val(equipment_parameter_newArr[2]);
            $(".equipment_parameter_newdd3").val(equipment_parameter_newArr[3]);
            $(".equipment_parameter_newdd4").val(equipment_parameter_newArr[1]);
            $(".equipment_parameter_newdd5").val(equipment_parameter_newArr[4]);
            $(".equipment_parameter_newdd6").val(equipment_parameter_newArr[5]);
            $(".equipment_parameter_newdd7").val(equipment_parameter_newArr[6]);

            $(".equipment_parameter_newdd8").val(equipment_parameter_newArr[7]);
            $(".equipment_parameter_newPower input[value="+equipment_parameter_newArr[8]+"]")[0].checked=true;
            var defaultImg=$(".equipment_parameter_rightContentdv>ul:eq("+equipment_index+")").find(".equipment_parameter_img").attr("src");

            $(".pointpage_dv3").stop().show().find(".pointpage_psdtitleName").text("${deviceTypeName}> ${groupName}> 修改参数");
            $("#GC_show_file").attr("src", defaultImg);
            $(".equipment_parameter_newbtn1").removeAttr("disabled", "disabled");
            //修改参数  确认按钮解禁
            $(".equipment_parameter_newdd1").on("keyup keydown", function(){
                if(regName.test($(this).val())){
                    $(".equipment_parameter_newbtn1").removeAttr("disabled", "disabled");
                }else{
                    $(".equipment_parameter_newbtn1").attr("disabled", "disabled");
                }
            });
            //修改参数  确认
            var reg_empty=/^\s*$/;
            $(".equipment_parameter_newbtn1").unbind("click");
            $(".equipment_parameter_newbtn1").on("click", function(){

                var typeName = "${deviceParamType}";
                var groupName = "${groupId}";
                var name = $(".equipment_parameter_newdd1").val();
                var img="";
                var commId=$(".equipment_parameter_newdd8").val();
                var inVal = $(".equipment_parameter_newdd2").val();
                var outVal = $(".equipment_parameter_newdd3").val();
                var unit = $(".equipment_parameter_newdd4").val();
                var defVal = $(".equipment_parameter_newdd5").val();
                var remark = $(".equipment_parameter_newdd6").val();
                var urlVal = $(".equipment_parameter_newdd7").val();
                var isOperator = $(".equipment_parameter_newdd8").attr("name");
                var permissions = $(".equipment_parameter_newPower input:checked").val();
                var permissionsText = $(".equipment_parameter_newPower input:checked").next("label").text();
                //取出弹窗各输入框的值
                var equipment_parameter_newBrr=[
                    name,img,commId,inVal,outVal,unit,defVal,remark,urlVal,permissionsText
                ];

                if(reg_empty.test(unit)){
                    alert("参数单位不能为空");
                    return;
                }else if(reg_empty.test(inVal)){
                    alert("参数范围不能为空");
                    return;
                }else if(reg_empty.test(outVal)){
                    alert("参数范围不能为空");
                    return;
                }else if(reg_empty.test(defVal)){
                    alert("参数值不能为空");
                    return;
                } else if(reg_empty.test(commId)){
                    alert("命令编号不能为空");
                    return;
                }

                var forData = new FormData();
                forData.append("configParamId",configParam);
                forData.append("typeName",typeName);
                forData.append("groupName",groupName);
                forData.append("name",name);
                forData.append("inVal",inVal);
                forData.append("outVal",outVal);
                forData.append("unit",unit);
                forData.append("defVal",defVal);
                forData.append("commId",commId);
                forData.append("remark",remark);
                forData.append("urlVal",urlVal);
                forData.append("logoVal",defaultImg);
                forData.append("permissions",permissions);
                forData.append("isOperator",isOperator);
                forData.append('logo', $('#GC_logo_file')[0].files[0]);

                $.ajax({
                    url: "${basePath}v2/factory/param/editParam",
                    type: 'POST',
                    data: forData,
                    cache: false,
                    contentType: false,
                    processData: false,
                    error: function (response) {
                        $errorMsg(response);
                    },
                    success: function (ret) {
                        if(ret.result==true){
                            //将各输入框的值  重新给回页面
                            $(".equipment_parameter_rightContentdv>ul:eq("+equipment_index+")").find(".equipment_parameter_img").attr("src", ret.logo);
                            $(".equipment_parameter_rightContentdv>ul:eq("+equipment_index+")").children().eq(1).text(equipment_parameter_newBrr[0]);
                            $(".equipment_parameter_rightContentdv>ul:eq("+equipment_index+")").children().eq(3).text(equipment_parameter_newBrr[2]);
                            $(".equipment_parameter_rightContentdv>ul:eq("+equipment_index+")").children().eq(4).text(equipment_parameter_newBrr[5]);
                            $(".equipment_parameter_rightContentdv>ul:eq("+equipment_index+")").children().eq(5).children("span").eq(0).text(equipment_parameter_newBrr[3]);
                            $(".equipment_parameter_rightContentdv>ul:eq("+equipment_index+")").children().eq(5).children("span").eq(1).text(equipment_parameter_newBrr[4]);
                            $(".equipment_parameter_rightContentdv>ul:eq("+equipment_index+")").children().eq(6).text(equipment_parameter_newBrr[6]);
                            $(".equipment_parameter_rightContentdv>ul:eq("+equipment_index+")").children().eq(7).text(equipment_parameter_newBrr[9]).attr("name", permissions);
                            $(".equipment_parameter_rightContentdv>ul:eq("+equipment_index+")").children().eq(8).text(equipment_parameter_newBrr[7]);
                            $(".equipment_parameter_rightContentdv>ul:eq("+equipment_index+")").children().eq(9).text(equipment_parameter_newBrr[8]);
                            $(".pointpage_dv3").stop().hide();
                            $(".equipment_parameter_newdd1, .equipment_parameter_newdd2, .equipment_parameter_newdd3, .equipment_parameter_newdd4, .equipment_parameter_newdd5, .equipment_parameter_newdd6, .equipment_parameter_newdd7").val("");
                            equipment_parameter_newBrr=null;
                            $(".equipment_parameter_newImgFile").val("");
                            alert("参数修改成功", "cur");
                        }else{
                            alert(ret.info);
                        }
                    }
                });
            });
        });

    }
    equipment_parameterEdit();

    //equipment_factory_details.html    equipment_query_result.html
    //修改参数组名称  显示
    $(".equipment_parameter_addbtn3").on("click", function(){
        var equipment_factory_index=$(this).closest("div").find(".equipement_factory_details_editdv").attr("name");
        var equipment_rightp_text2=$(".equipment_rightp_text2[name="+equipment_factory_index+"]").text();
        $(".equipement_factory_details_editdv[name="+equipment_factory_index+"] .equipement_factory_details_editval").val(equipment_rightp_text2);
        $(".equipement_factory_details_editdv[name="+equipment_factory_index+"]").css({"transition":"all 0.2s", "left":"0","opacity":"1"});
    });
    //修改参数组名称  取消
    $(".equipement_factory_details_editbtn2").on("click", function(){
        var equipment_factory_index=$(this).closest("div").attr("name");
        $(".equipement_factory_details_editdv[name="+equipment_factory_index+"]").css({"transition":"all 0.2s", "left":"950px","opacity":"0"});
    });
    //修改参数组名称  确认
    $(".equipement_factory_details_editbtn1").on("click", function(){
        var equipment_factory_index=$(this).closest("div").attr("name");
        var equipment_rightp_text1=$(".equipment_rightp_text1").text();
        var equipment_rightp_text2=$(".equipment_rightp_text2[name="+equipment_factory_index+"]").text();
        var equipement_factory_details_editval=$(".equipement_factory_details_editdv[name="+equipment_factory_index+"] .equipement_factory_details_editval").val();
        var groupId = "${groupId}";
        $(".equipment_parameter .equipment_parMorebtn").each(function(index){
            if($(".equipment_parameter .equipment_parMorebtn:eq("+index+")").text()==equipment_rightp_text1){
                $(".equipment_parMoreul:eq("+index+") a").each(function(index2){
                    if($(".equipment_parMoreul:eq("+index+") a:eq("+index2+")").text()==equipment_rightp_text2){
                        $.ajax({
                            url: "${basePath}v2/factory/param/updateParamGroupName",
                            type: 'POST',
                            data: {"paramGroupId": groupId,"groupName":equipement_factory_details_editval},
                            error: function (response) {
                                $errorMsg(response);
                            },
                            success: function (ret) {
                                if(ret.result==true){
                                    //重新赋值
                                    $(".equipment_rightp_text2[name="+equipment_factory_index+"]").text(equipement_factory_details_editval);
                                    $(".equipment_parMoreul:eq("+index+") a:eq("+index2+")").text(equipement_factory_details_editval);
                                    $(".equipement_factory_details_editdv[name="+equipment_factory_index+"]").css({"transition":"all 0.2s", "left":"950px","opacity":"0"});
                                }else{
                                    alert(ret.info);
                                }
                            }
                        });
                    }
                })
            }
        })
    });

    //增加参数组  确认
    $(".equipment_parameter_cfm").on("click", function(){
        var equipment_parameter_addval =$(this).parents(".pointpage_psdval").find(".pointpage_value").val();
        var equipmentType=$(this).parents(".page").find(".page_right").find(".equipment_rightp_text1").children("span").text();

        if(regName.test(equipment_parameter_addval)){
            var typeName = '${deviceParamType}';
            $.ajax({
                url: "${basePath}v2/factory/param/addParamGroup",
                type: 'POST',
                data: {"typeName":typeName,"groupName":equipment_parameter_addval},
                error: function (response) {
                    $errorMsg(response);
                },
                success: function (ret) {
                    if(ret.result==true){
                        alert("参数组添加成功", "cur");

                        $(".page").find(".equipment_parameterMore").find(":contains("+equipmentType+")").next("ul").append("<li><a href='javascript:;' name='"+ret.groupId+"'>"+equipment_parameter_addval+"</a></li>");

                        $(".pointpage_dv2").stop().hide();

                        equipment_parameterThird();
                    }else{
                        alert(ret.info);
                    }
                }
            });
        }
    });

</script>